import AMap from 'AMap';

// 字符串颜色转数字颜色
const Color = require('color-js');

// 路线轨迹缩放比例
const zoomStyleMapping = {
  13: 0,
  14: 0,
  15: 0,
  16: 0,
  17: 0,
  18: 0,
  19: 0,
  20: 0
};

// 百度转高德
const bd2decrypt = function (lng, lat) {
  var X_PI = Math.PI * 3000.0 / 180.0;
  var x = lng - 0.0065;
  var y = lat - 0.006;
  var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
  var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
  return {lng: z * Math.cos(theta), lat: z * Math.sin(theta)};
};

const boundArray2Object = function (boundArray, color = '#b5c81770', height = 2000, type = 'Prise') {
  var bounds = [];
  boundArray.forEach(p => {
    if (p instanceof Array) {
      var gdPoint = bd2decrypt(p[0], p[1]);
      bounds.push(new AMap.LngLat(gdPoint.lng, gdPoint.lat));
    } else {
      bounds.push(p);
    }
  });
  var wall = new AMap.Object3D[type]({
    path: bounds,
    height: height,
    color: color
  });
  wall.backOrFront = 'both';
  wall.transparent = true;
  return wall;
};

// 在地图上添加轨迹线
const path2Polyline = function (path, lineColor = '#1104c7', lineWeight = 10) {
  var polyline = new AMap.Polyline({
    path: path,
    showDir: true,
    strokeColor: lineColor, // 线颜色
    strokeWeight: lineWeight // 线宽
  });
  return polyline;
};

// 在地图上添加轨迹点
const path2Marker = function (stratPostion, imgPath, imgSize, imgAncher, lableName = '', labelOffset = [-35, 0], position = 'BM') {
  var marker = new AMap.ElasticMarker({
    position: stratPostion,
    zooms: [12, 20],
    styles: [{
      icon: {
        img: imgPath,
        size: imgSize, // 可见区域的大小
        ancher: imgAncher, // 锚点
        fitZoom: 13, // 最合适的级别
        scaleFactor: 2, // 地图放大一级的缩放比例系数
        maxScale: 4, // 最大放大比例
        minScale: 1 // 最小放大比例
      },
      label: {
        content: lableName,
        offset: labelOffset,
        position: position
      }
    }],
    zoomStyleMapping: zoomStyleMapping 
  });
  return marker;
};

// 添加雷达
const addRadar = function (position, radius, color = '#00ff00') {
  var radar = new AMap.Object3D.Mesh();
  radar.transparent = true;
  radar.backOrFront = 'front';
  var p = new AMap.LngLat(position[0], position[1]);
  var geometry = radar.geometry;
 
  var unit = 1;
  var range = 500;
  var count = range / unit;
  var colorObj = Color(color);
  for (var i = 0; i < count; i += 1) {
    var angle1 = i * unit * Math.PI / 180;
    var angle2 = (i + 1) * unit * Math.PI / 180;

    var p1x = Math.cos(angle1) * radius;
    var p1y = Math.sin(angle1) * radius;
    var p2x = Math.cos(angle2) * radius;
    var p2y = Math.sin(angle2) * radius;

    geometry.vertices.push(0, 0, 0);
    geometry.vertices.push(p1x, p1y, 0);
    geometry.vertices.push(p2x, p2y, 0);
    
    var opacityStart = 1 - Math.pow(i / count, 0.3);
    var opacityEnd = 1 - Math.pow((i + 1) / count, 0.3);
    geometry.vertexColors.push(colorObj.red, colorObj.green, colorObj.blue, opacityStart);
    geometry.vertexColors.push(colorObj.red, colorObj.green, colorObj.blue, opacityStart);
    geometry.vertexColors.push(colorObj.red, colorObj.green, colorObj.blue, opacityEnd);
  }
  radar.position(p);

  return radar;
};

export default {
  boundArray2Object,
  boundArray2Wall: (boundArray, color, height) => boundArray2Object(boundArray, color, height, 'Wall'),
  boundArray2Prism: (boundArray, color, height) => boundArray2Object(boundArray, color, height, 'Prism'),
  path2Polyline,
  path2Marker,
  addRadar
};
